cookiejs.get   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 6
c 2
b 0
f 2
nc 2
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 2
rs 10
nop 1
1
/*!
2
 * cookiejs.js | v1.0.1 | cookiejs object for setting/getting/removing cookies
3
 * Copyright (c) 2017 Eric Zieger (MIT license)
4
 * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
5
 */
6
7
/**
8
 * throwTypeError
9
 *
10
 * @param {String} sName
11
 * @param {String} sType
12
 *
13
 * @throws {TypeError}
14
 */
15 1
var throwTypeError = function(sName, sType) {
16 24
  throw new TypeError(sName + ' is not of type ' + sType);
17
};
18
19 1
/**
20
 * check variable (specifically sCookieName) for a non-falsy value and it's type to be string
21
 *
22
 * @param {String} sCookieName
23
 *
24
 * @throws {TypeError}
25
 */
26
var checkCookieName = function(sCookieName) {
27
  isString(sCookieName, 'sCookieName');
28
  if (!sCookieName) {
29
    throwTypeError('sCookieName', 'string');
30
  }
31
};
32 33
33
/**
34 33
 * check variable type to be a string
35
 *
36 33
 * @param {String} variableToTest
37 4
 * @param {String} name
38
 *
39
 * @throws {TypeError}
40 29
 */
41 4
var isString = function(variableToTest, name) {
42
  if (typeof variableToTest !== 'string') {
43
    throwTypeError(name, 'string');
44 25
  }
45 7
};
46
47 18
/**
48 5
 * check variable type to be an object
49
 *
50
 * @param {String} variableToTest
51 13
 *
52 20
 * @returns {bool}
53 4
 */
54 1
var isObject = function(variableToTest) {
55 1
  return typeof variableToTest === 'object' && !Array.isArray(variableToTest);
56
};
57 3
58
var cookiejs = {
59 16
  global: typeof window !== 'undefined' ? document : { cookie: '' },
60 7
61
  /**
62
   * sets or overwrites a cookie
63 9
   *
64
   * @param {String} sCookieName - the name of the cookie you want to set
65
   * @param {String} sValue - the value you want to set
66
   * @param {String} oAttributes - options e.g. domain, path, expires
67 10
   *
68
   * @throws {TypeError} if argument sCookieName is empty or not a string
69
   */
70
  set: function(sCookieName, sValue, oAttributes) {
71
    var sAttributes = '';
72
73
    sValue = sValue || '';
74
75
    checkCookieName(sCookieName);
76
77
    isString(sValue, 'sValue');
78
79
    if (oAttributes === undefined) {
80
      sAttributes += '; path=/';
81
    } else {
82
      if (!isObject(oAttributes)) {
83
        throwTypeError('oAttributes', 'object');
84 2
      }
85
86 4
      Object.keys(oAttributes).forEach(function(sAttr) {
87 1
        var cookiePropValue = oAttributes[sAttr];
88
89
        if (sAttr === 'secure') {
90 1
          if (cookiePropValue !== true && cookiePropValue !== 'true') {
91 2
            throwTypeError(sAttr, 'boolean');
92
          }
93 2
94 1
          sAttributes += ';' + sAttr;
95 1
        } else {
96
          isString(cookiePropValue, sAttr);
97
          sAttributes += ';' + sAttr + '=' + cookiePropValue;
98 1
        }
99
      });
100
    }
101 1
102
    cookiejs.global.cookie =
103
      encodeURIComponent(sCookieName) +
104
      '=' +
105
      encodeURIComponent(sValue) +
106
      sAttributes;
107
  },
108
109
  /**
110
   * returns the value of a cookie
111
   *
112
   * @param {String} sCookieName
113
   *
114
   * @throws {TypeError}
115
   *
116 4
   * @returns {String|Boolean}
117
   */
118 4
  get: function(sCookieName) {
119
    var gCookieValue = false;
120
121
    checkCookieName(sCookieName);
122 3
123
    cookiejs.global.cookie.split('; ').some(function(sCookie) {
124
      var aCookie = sCookie.split('=');
125 4
126
      if (decodeURIComponent(aCookie[0]) === sCookieName) {
127
        gCookieValue = decodeURIComponent(aCookie[1]);
128
        return true;
129 1
      }
130
131
      return false;
132
    });
133
134
    return gCookieValue;
135
  },
136
137
  /**
138
   * removes a specific cookie
139
   *
140
   * oAttributes must contain the correct path and domain it won't
141
   * remove the cookie
142
   *
143
   * @param {String} sCookieName
144
   * @param {Object} oAttributes - options e.g. domain, path, expires
145
   *
146
   * @throws {TypeError}
147
   */
148
  remove: function(sCookieName, oAttributes) {
149
    var oRemoveAttributes = oAttributes || {};
150
151
    if (isObject(oRemoveAttributes)) {
152
      oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
153
    }
154
155
    cookiejs.set(sCookieName, '', oRemoveAttributes);
156
  }
157
};
158
159
module.exports = cookiejs;
160